package com.nf.health.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;
import com.nf.health.app.models.HistoryVo;
import com.nf.health.app.models.OxyDeviceDataBean;
import com.nf.health.app.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class OxygenHistoryDataAdapter extends BaseAdapter {

	String[] Str;

	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

	private Context context;
	private List<OxyDeviceDataBean> doctors;



	public OxygenHistoryDataAdapter(Context context, List<OxyDeviceDataBean> result) {
		this.context = context;
		this.doctors = result;
	}
	public void setOxygenHistoryDataRefresh(List<OxyDeviceDataBean> result) {
		this.doctors = result;notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (doctors != null) {
			return doctors.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return doctors.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(final int position, View view, ViewGroup viewGroup) {
		final ViewHolder holder;
		if (view == null) {
			view = View.inflate(context, R.layout.my_blood_oxygen_history_item, null);
			holder = new ViewHolder();
			holder.history_oxygen_time = (TextView) view.findViewById(R.id.history_oxygen_time);
			holder.history_oxygen = (TextView) view.findViewById(R.id.history_oxygen);
			holder.history_heartRate = (TextView) view.findViewById(R.id.history_heartRate);
			holder.swipeView_oxygen  = (SwipeView) view.findViewById(R.id.swipeView_oxygen);
			holder.delete  = view.findViewById(R.id.delete);
			view.setTag(holder);
			
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(position, "");
				unClosedSwipeView.clear();
			}
		});
		holder.swipeView_oxygen.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {

			@Override
			public void onSwiping(SwipeView swipingSwipeView) {
				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					closeAllOpenedSwipeView();
				}

				if (!unClosedSwipeView.contains(swipingSwipeView)) {
					unClosedSwipeView.add(swipingSwipeView);
				}

			}

			@Override
			public void onOpen(SwipeView openedSwipeView) {
				for (int i = 0; i < unClosedSwipeView.size(); i++) {
					if (unClosedSwipeView.get(i) != openedSwipeView) {
						unClosedSwipeView.get(i).close();
					}
				}
				if (!unClosedSwipeView.contains(openedSwipeView)) {
					unClosedSwipeView.add(openedSwipeView);
				}
			}

			@Override
			public void onClose(SwipeView closedSwipeView) {
				unClosedSwipeView.remove(closedSwipeView);
			}
		});
		OxyDeviceDataBean oxyDeviceDataBean = doctors.get(position);
		holder.history_oxygen_time.setText(DateUtil.gsubTimeForData(oxyDeviceDataBean.getPosttime(),"MM-dd HH:mm:ss"));
		//holder.history_oxygen_time.setText(String.valueOf(oxyDeviceDataBean.getPosttime()));
		holder.history_heartRate.setText(String.valueOf(oxyDeviceDataBean.getRate()));
		holder.history_oxygen.setText(String.valueOf(oxyDeviceDataBean.getSp2()));


		return view;
	}

	class ViewHolder {
		TextView history_oxygen_time, history_heartRate, history_oxygen;
		SwipeView swipeView_oxygen;
		View delete;
	}
	
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){

		this.listener = listener;
	}
	public void closeAllOpenedSwipeView() {
        for (int i = 0; i < unClosedSwipeView.size(); i++) {
            if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
                unClosedSwipeView.get(i).close();
            }
        }
    }
	
}
